package com.shiroexploit.util;

import org.apache.commons.codec.binary.Base64;
import org.apache.commons.codec.binary.Hex;

import java.util.HashMap;
import java.util.Map;

public class HttpRequestInfo {
    private String requestMethod;
    private String requestURL;
    private String rememberMeCookie;
    private String requestBody;
    private Map<String, String> headers = new HashMap<>();
    private String original;


    public String getOriginal() {
        return original;
    }

    public void setOriginal(String original) {
        this.original = original;
    }

    public String getRequestMethod() {
        return requestMethod;
    }

    public void setRequestMethod(String requestMethod) {
        this.requestMethod = requestMethod;
    }

    public String getRequestURL() {
        return requestURL;
    }

    public void setRequestURL(String requestURL) {
        this.requestURL = requestURL;
    }

    public String getRememberMeCookie() {
        return rememberMeCookie;
    }

    public void setRememberMeCookie(String rememberMeCookie) {
        this.rememberMeCookie = rememberMeCookie;
    }

    public String getRequestBody() {
        return requestBody;
    }

    public void setRequestBody(String requestBody) {
        this.requestBody = requestBody;
    }

    public Map<String, String> getHeaders() {
        return headers;
    }

    public void setHeaders(Map<String, String> headers) {
        this.headers = headers;
    }

    @Override
    public String toString() {
        return "HttpRequestInfo{" +
                "requestMethod='" + requestMethod + '\'' +
                ", requestURL='" + requestURL + '\'' +
                ", rememberMeCookie='" + rememberMeCookie + '\'' +
                ", requestBody='" + requestBody + '\'' +
                ", headers=" + headers +
                ", original='" + original + '\'' +
                '}';
    }

    public void parse(String body, boolean bool) throws Exception {
        setOriginal(body);
        //split操作可能会产生异常，所以用 try...catch 进行捕获
        //这里把异常抛出来的目的是为了数据校验操作，抛出异常，说明数据输入格式不正确
        try{
            body = body.trim();
            String[] parts = body.split("\r\n\r\n|\r\r|\n\n");
            if(parts.length == 2){
                //说明为POST请求，并且有请求体
                setRequestBody(parts[1]);
            }

            //请求头部
            String[] lines = parts[0].split("\r\n|\r|\n");
            //请求起始行
            String requestLine = lines[0].trim();
            parts = requestLine.split("\\s+");
            String requestMethod = parts[0];
            setRequestMethod(requestMethod);
            String requestURI = parts[1];

            Map<String, String> headers = new HashMap<>();
            for(int i = 1; i < lines.length; i++){
                String[] pair = lines[i].trim().split(":",2);
                headers.put(pair[0].trim(), pair[1].trim());
            }
            setHeaders(headers);

            if(headers.get("Host") == null){
                throw new Exception();
            }

            String url = (bool ? "https://" : "http://");
            url += headers.get("Host").trim() + requestURI;
            setRequestURL(url);

            String cookie = headers.get("Cookie");
            if(cookie != null){
                String[] cookies = cookie.trim().split(";");
                for(String temp : cookies){
                    temp = temp.trim();
                    if(temp.startsWith(Config.getInstance().getRememberMeCookieName() + "=")){
                        setRememberMeCookie(Hex.encodeHexString(Base64.decodeBase64(temp.split("=",2)[1].trim())));
                    }
                }
            }
        }catch(Exception e){
            e.printStackTrace();
            throw new Exception();
        }
    }
}
